package com.cqupt.tr.ttuauth.controller;


import com.cqupt.tr.Utils.HttpClientUtil;
import com.cqupt.tr.Utils.ObjectJsonTransFunction;
import com.cqupt.tr.Utils.callback.OnMessageCallback;
import com.cqupt.tr.bean.BaseMessage;
import com.cqupt.tr.bean.pojo.TTU;
import com.cqupt.tr.bean.vo.TTUVO;
import com.cqupt.tr.ttuauth.service.TTUAuthService;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.net.URISyntaxException;
import java.text.SimpleDateFormat;
import java.util.Base64;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


@RestController
public class TTUAuthController {

    @Autowired
    private Map<String,MqttClient> mqttClientMap;
    @Autowired
    private TTUAuthService ttuAuthService;

    @PostMapping("/addTTU")
    public String addTTU(@RequestBody TTUVO ttuvo) throws MqttException {
        String ttunode = ttuvo.getTtunode();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String nowTime = df.format(new Date());
        TTU ttu = new TTU(ttuvo.getTtuname(),nowTime,ttunode,0,ttuvo.getDepartment(),ttuvo.getmRid(),1);
        BaseMessage baseMessage = ttuAuthService.addNewTTU(ttu);
        if(baseMessage.getCode().equals("0")){
            MemoryPersistence persistence = new MemoryPersistence();
            String broker = "tcp://"+ ttunode +":1883";
            MqttClient admin_client = new MqttClient(broker, "admin_client", persistence);
            MqttConnectOptions connectOptions = new MqttConnectOptions();
            connectOptions.setUserName("admin"+ttunode);
            connectOptions.setPassword("public".toCharArray());
            //保留会话
            connectOptions.setCleanSession(true);
            //设置回调
            admin_client.setCallback(new OnMessageCallback());
            //建立连接
            admin_client.connect(connectOptions);
            mqttClientMap.put(ttunode,admin_client);
            System.out.print(mqttClientMap.get(ttunode).isConnected());
        }

        return ObjectJsonTransFunction.toJsonStr(baseMessage);
    }

//    @GetMapping("/mqttTest")
//    public String mqttTest() throws MqttException {
//
//            mqttClient.subscribe("/aaa/bbb/ccc");
//
//            //发布消息
//            MqttMessage mqttMessage = new MqttMessage("nihao".getBytes());
//            mqttMessage.setQos(1);
//            mqttClient.publish("/fuck",mqttMessage);
//            System.out.print("okokookokokok");
//        return  "yesyesyes";
//    }

    @GetMapping("/getClients")
    public String getClients() throws URISyntaxException {

        Map<String,Object> map = new HashMap<>();
        map.put("Authorization","Basic " + Base64.getUrlEncoder().encodeToString(("admin" + ":" + "public").getBytes()));
        String s = HttpClientUtil.httpGetRequest2("http://192.168.213.128:8081/api/v4", map);
        return  s;
    }

}





